<script setup>
import { reactive, ref, toRefs, onMounted } from 'vue';
import { Search } from '@element-plus/icons-vue';
import Pagination from '@/components/Pagination/index.vue';
import { queryComponentsProductsPageList } from '@/server/api/biz.js';
import { downloadWebFile } from '@/utils/common.js';
import { useI18n } from 'vue-i18n';
const { t } = useI18n();
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '',
  },
});

const emits = defineEmits(['update:modelValue', 'close', 'selected']);

const currentRow = ref(null);
const list = ref([]); // 已下单订单列表
const total = ref(0);
const data = reactive({
  queryParams: {
    page: 1,
    limit: 10,
    mfrName: '',
    insteadProSno: '',
    productDescription: '',
    isUseAlternative: 1,
  },
});
const { queryParams } = toRefs(data);
const radio = ref();

onMounted(() => {
  getList();
});

const onClose = () => {
  emits('update:modelValue', false);
};

// 下载
const handleDownload = (url) => {
  // downloadWebFile(url);
  window.open(url, '_blank');
};

/**
 * 获取已下单订单列表
 */
const getList = async () => {
  const res = await queryComponentsProductsPageList({
    ...data.queryParams,
  });
  if (res.code === 200) {
    list.value = res.data.list;
    total.value = res.data.totalRow;
  } else {
    ElMessage.error(res.msg);
  }
};

// 查询
const handleQuery = () => {
  data.queryParams.page = 1;
  getList();
};

// 重置
const handleReset = () => {
  data.queryParams.mfrName = '';
  data.queryParams.insteadProSno = '';
  data.queryParams.productDescription = '';
  handleQuery();
};

let fileId = ref();
const handleCurrentChange = (row) => {
  currentRow.value = row;
  fileId.value = row?.id;
};

/**
 * 确认选择
 */
const handleSubmit = () => {
  if (!currentRow.value) {
    return ElMessage.warning(t('mall.select_materials'));
  }
  onClose();
  emits('selected', currentRow.value);
};
</script>

<template>
  <div class="select-modal">
    <!-- 物料选择弹窗 -->
    <el-dialog
      v-model="props.modelValue"
      :title="props.title"
      center
      width="1300"
      :before-close="onClose"
    >
      <el-row type="flex" justify="end">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
          <el-form-item prop="mfrName" style="margin-bottom: 12px">
            <el-input
              v-model="queryParams.mfrName"
              :placeholder="$t('mall.enter_brand')"
              clearable
              style="width: 254px"
              @keyup.enter="handleQuery"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="insteadProSno" style="margin-bottom: 12px">
            <el-input
              v-model="queryParams.insteadProSno"
              :placeholder="$t('mall.enter_component_model')"
              clearable
              style="width: 254px"
              @keyup.enter="handleQuery"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="productDescription" style="margin-bottom: 12px">
            <el-input
              v-model="queryParams.productDescription"
              :placeholder="$t('smt.yourcomponentkeywords')"
              clearable
              style="width: 254px"
              @keyup.enter="handleQuery"
            >
            </el-input>
          </el-form-item>
          <el-form-item style="margin-bottom: 12px">
            <el-button class="m-l-15" type="primary" @click="handleQuery">{{
              $t('om.Search')
            }}</el-button>
            <el-button class="m-l-15" @click="handleReset">{{
              $t('mall.reset')
            }}</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-table :data="list" border style="width: 100%" @current-change="handleCurrentChange">
        <el-table-column width="50px" align="center">
          <template #default="scope">
            <el-radio v-model="radio" :label="scope.row.id">
              <span></span>
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column
          prop="fileName"
          :label="`${$t('mall.model')}/${$t('mall.brand')}/${$t('mall.package')}`"
          width="200"
        >
          <template #default="scope">
            <h5>{{ scope.row.proSno }}</h5>
            <p>{{ t('mall.brand') }}：{{ scope.row.mfrName }}</p>
            <p>{{ t('mall.package') }}：{{ scope.row.footprint }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="fileName" :label="$t('mall.component_parameters')" width="250">
          <template #default="scope">
            <p>{{ scope.row.detailedDescription }}</p>
            <el-link type="primary" @click="handleDownload(scope.row.manufacturerPageUrl)"
              >{{ t('mall.specification_sheet') }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column prop="sqp" label="SPQ" />
        <el-table-column prop="moq" label="MOQ" />
        <el-table-column prop="stock" :label="$t('mall.inventory')" />
        <el-table-column :label="$t('mall.price')" width="200">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.priceLadderFormat" :key="index">
              {{ item.qty }}+： ¥ {{ item.price }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="manufacturerLeadWeeks" :label="`${$t('mall.logout_prompt')}:`" />
        <el-table-column prop="fileName" :label="$t('mall.date_code')" width="100">
        </el-table-column>
        <el-table-column prop="productUrl" :label="$t('mall.product_link')" width="100">
          <template #default="scope">
            <el-link type="primary" @click="handleDownload(scope.row.productUrl)">{{
              $t('mall.view_details')
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="isUseAlternative"
          :label="$t('mall.accept_alternative_components')"
          width="100"
        >
          <template #default="scope">
            <span>{{
              scope.row.isUseAlternative == 1 ? $t('mall.accept') : $t('mall.do_not_accept')
            }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.limit"
        @pagination="getList"
      />

      <template #footer>
        <span class="dialog-footer">
          <el-button palin @click="onClose">{{ $t('smt.Cancel') }}</el-button>
          <el-button type="primary" @click="handleSubmit()"> {{ $t('smt.Define') }} </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.icon {
  cursor: pointer;
}
</style>
